package com.fita.api.dto.request;

import java.math.BigInteger;

import org.apache.commons.lang3.StringUtils;
import org.opentravel.ota._2003._05.CompanyNameType;
import org.opentravel.ota._2003._05.CountryNameType;
import org.opentravel.ota._2003._05.ItemSearchCriterionType;
import org.opentravel.ota._2003._05.OTAVehLocSearchRQ;
import org.opentravel.ota._2003._05.POSType;
import org.opentravel.ota._2003._05.SourceType;
import org.opentravel.ota._2003._05.SourceType.RequestorID;

import com.fita.api.dto.common.CommonVariable;

/**
 * 
 *
 * @author LZJ
 *
 */
public class LocationSearchRequest {
	
	 /**
     * 车辆可用状态
     * 
     *  - Available=可用车辆
     *  - All=所有车辆
     */	
	private String status;
	
	private String refPoint;
	
	private String countryCode;
	
	private String postalCode;
	
	private String countryName;
	
	
	private String vendorCode;
    
	
    
    
    private String env;
    
    

	/**
     * The default constructor
     */
    public LocationSearchRequest() {
    	
    }
    
    /**
     * @param env
     * @return
     */
    public LocationSearchRequest initialize(String env) {
        this.env = env;
        return this;
    }
    
    
    
    /**
     * @return
     * 
     * 校验获取的参数
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(status) &&
               refPoint != null && 
               countryCode != null &&
               postalCode !=null &&
               countryName !=null&&
               vendorCode != null;
    }
    
    
    
    /**
     * @return
     *  
     */
    public OTAVehLocSearchRQ geneLocSecRq() {
        
    	OTAVehLocSearchRQ rq = new OTAVehLocSearchRQ();
        
        rq.setPrimaryLangID(CommonVariable.LANGUAGE);
        rq.setTarget(env);
        rq.setVersion(CommonVariable.VERSION);
        rq.setTransactionIdentifier("100000001");
        rq.setSequenceNmbr(new BigInteger("1"));
        
        POSType posType = this.genecanclePos();
        rq.setPOS(posType);
        
        ItemSearchCriterionType criterion = this.genecancleRQCriterion();
        CompanyNameType vendor = this.genevendor();
        
        rq.getVehLocSearchCriterion().add(criterion);       
        rq.setVendor(vendor);
        
       
        
        return rq;
        
    }
    
    
    
    
    /**
     * @return
     */
    private POSType genecanclePos() {
        
        POSType pos = new POSType();
        
        // source1
        SourceType sourceType1 = new SourceType();
        sourceType1.setISOCountry("CN");
        
        // requestID
        RequestorID requestorID = new RequestorID();
        requestorID.setType(CommonVariable.REQUESTOR_TYPE);
        requestorID.setID("XMLRTA");
        
        // companyName
        CompanyNameType companyName = new CompanyNameType();
        if(StringUtils.equalsAnyIgnoreCase(env, "Production")) {
            companyName.setCode("CU");
            companyName.setCompanyShortName("CNTVLSVCXML");
        } else if(StringUtils.equalsAnyIgnoreCase(env, "Testing")) {
            companyName.setCode("EX");
            companyName.setCompanyShortName("EHIXMLTEST");
        }
        
        requestorID.setCompanyName(companyName);
        sourceType1.setRequestorID(requestorID);
        
        // source2
        SourceType sourceType2 = new SourceType();
        
        // requestID
        requestorID = new RequestorID();
        requestorID.setType("4");
        requestorID.setID("00000000");
        requestorID.setIDContext("IATA");
        
        sourceType2.setRequestorID(requestorID);
        
        pos.getSource().add(sourceType1);
        pos.getSource().add(sourceType2);
        
        return pos;
        
    }
    
    
    private ItemSearchCriterionType genecancleRQCriterion() {
    	
    	ItemSearchCriterionType criterion = new ItemSearchCriterionType();
    	
    	ItemSearchCriterionType.Address address = new ItemSearchCriterionType.Address();  	
    	
    	ItemSearchCriterionType.RefPoint  point = new ItemSearchCriterionType.RefPoint();
    	
    	//根据传进来的参数判断使用哪一个模板
    	if(StringUtils.isNotBlank(refPoint) && StringUtils.isNotBlank(countryCode)) {
    		
    		point.setValue(this.refPoint);    	
        	point.setCountryCode(this.countryCode);
        	criterion.getRefPoint().add(point);  
    		
    	}else if (StringUtils.isNotBlank(postalCode) && StringUtils.isNotBlank(countryName)) {
    		
    		address.setPostalCode(this.postalCode);
        	
        	CountryNameType contryName = new CountryNameType();
        	contryName.setCode(countryName);
        	
        	address.setCountryName(contryName);

        	
        	criterion.setAddress(address);
    		
    		
    	}
    	

    	
    	
    	
    	
    	return criterion;
    	
    }
    
    private CompanyNameType genevendor() {
    	
    	CompanyNameType vendor = new CompanyNameType();
    	
    	vendor.setCode(this.vendorCode);
    	
    	
    	return  vendor;
    }
    
    
    
  

	public String getEnv() {
		return env;
	}

	public void setEnv(String env) {
		this.env = env;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRefPoint() {
		return refPoint;
	}

	public void setRefPoint(String refPoint) {
		this.refPoint = refPoint;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	
	

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	public String getVendorCode() {
		return vendorCode;
	}

	public void setVendorCode(String vendorCode) {
		this.vendorCode = vendorCode;
	}

	
	
    
}
